package com.sea.base.ext.global

import androidx.lifecycle.Lifecycle
import com.sea.base.utils.LifecycleCountdownTimer

/**
 * 创建一个基于生命周期的倒计时，按秒计算
 */
inline fun createSecondsCountdownTimer(
    lifecycle: Lifecycle,
    maxSeconds: Int,
    crossinline onTickCallback: LifecycleCountdownTimer.(Int) -> Unit,
    crossinline onFinishCallback: LifecycleCountdownTimer.() -> Unit,
): LifecycleCountdownTimer {
    return LifecycleCountdownTimer(lifecycle, maxSeconds * 1000L, 1000L, onTickCallback = {
        val currentSeconds = (currentDiffTimeMillis / 1000L).toInt()
        onTickCallback(maxSeconds - currentSeconds)
    }, onFinishCallback = {
        onFinishCallback()
    })
}

inline val LifecycleCountdownTimer.currentDiffTimeSeconds get() = (currentDiffTimeMillis / 1000L).toInt()